
import axios from 'axios';
const baseURL = 'http://localhost:5001/api_1_0';

export function getUser(params) {
  function getUserInfo(params) {
    return axios.get(`${baseURL}/consulting`, {
      params: {
        ...params
      }
    })
    .then(response => response)
    .catch(error => {
      console.error('Error during fetching user info:', error);
      throw error;
    });
  }
  
  function getConsulterName(con_id) {
    return axios.get(`${baseURL}/consulter/${con_id}`)
      .then(response => {
        if (response.data && response.data.code === "2000" && response.data.data) {
          return response.data.data[0].con_name; // 确保返回的是姓名字符串
        } else {
          throw new Error('Invalid response or data not found');
        }
      })
      .catch(error => {
        console.error(`Error fetching consulter name for con_id ${con_id}:`, error);
        return null; // 在 catch 中返回 null 或适当的默认值
      });
  }
  
  
  function getPsychologistName(psy_id) {
    return axios.get(`${baseURL}/psyConsultant/${psy_id}`)
      .then(response => {
        if (response.data && response.data.code === "2000" && response.data.data) {
          return response.data.data[0].psy_name; // 确保返回的是姓名字符串
        } else {
          throw new Error('Invalid response or data not found');
        }
      })
      .catch(error => {
        console.error(`Error fetching psychologist name for psy_id ${psy_id}:`, error);
        return null; // 在 catch 中返回 null 或适当的默认值
      });
  }
  
  function getAllNames(userInfo) {
    const userInfoArray = Array.isArray(userInfo.data.data) ? userInfo.data.data : [userInfo.data.data];
  
    const con_ids = userInfoArray.map(item => item.con_id);
    const psy_ids = userInfoArray.map(item => item.psy_id);
    return Promise.all([
      Promise.all(con_ids.map(id => getConsulterName(id))),
      Promise.all(psy_ids.map(id => getPsychologistName(id)))
    ]).then(([consulterNames, psychologistNames]) => {
      return userInfoArray.map((user, index) => ({
        ...user,
        con_name: consulterNames[index],
        psy_name: psychologistNames[index] || "Unknown" // 提供默认值
      }));
    });
  }
  
  return getUserInfo(params)
    .then(response => {
      // 使用response.data调用getAllNames
      //console.log(getAllNames(response))
      return getAllNames(response);
    })
    .catch(error => {
      throw error;
    });
}

export function updateUser(user) {
    // 创建一个 FormData 实例
    let formData = new FormData();
    
    // 将用户数据添加到 FormData 实例中
    formData.append('status', user.status);

    let data={status:user.status}
    
    // 发送 POST 请求
    return axios.put(`${baseURL}/consulting/${user.consult_id}`, data, {
      headers: {
        // 告诉 axios 我们发送的是 FormData
        'Content-Type': 'application/json'
      }
    })
    .then(response => response.data)
    .catch(error => {
      console.error('Error during creating user:', error);
      throw error;
    });
  }

  export function deleteUser(id) {
    return axios.delete(`${baseURL}/psyConsultant/${id}`)
      .then(response => response.data)
      .catch(error => {
        console.error('Error during deleting user:', error); // 输出错误信息到控制台
        throw error;
      });
  }